package com.ymy.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class DateUtil {

    public static String formatStr(String dateStr){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try{
            Date date = simpleDateFormat.parse(dateStr);
            simpleDateFormat= new SimpleDateFormat("yyyyMMddHH");
            return simpleDateFormat.format(date);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }
   public static Date toDayStartHour(Date date){
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(date);
       calendar.set(Calendar.HOUR_OF_DAY,0);
       calendar.set(Calendar.MINUTE,0);
       calendar.set(Calendar.SECOND,0);
       calendar.set(Calendar.MILLISECOND,0);
       Date start = calendar.getTime();
       return start;
   }
        public static  Date addDateMinutes(Date date,int minutes){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE,minutes);
        date = calendar.getTime();
        return date;
        }

        public static Date addDateHour(Date date, int hour){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,hour);
        date = calendar.getTime();
        return date;
        }

        public static List<Date> getDateMenus(){
        List<Date> dates = new ArrayList<>();
        Date date = toDayStartHour(new Date());
        for (int i = 0;i <12;i++){
            dates.add(addDateHour(date,i*2));
        }
        Date now = new Date();
        for (Date cdate : dates){
            if (cdate.getTime() <= now.getTime() && now.getTime() < addDateHour(cdate,2).getTime()){
                now = cdate;
                break;
            }
        }
        List<Date> dateList = new ArrayList<>();
        for (int i = 0; i <= 5;i++){
            dateList.add(addDateHour(now,i*2));
        }
        return dateList;
        }
    public static String date2Str(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
        return simpleDateFormat.format(date);
    }

}
